
import { createRouter, createWebHashHistory } from 'vue-router'
import Login from '../views/login/index.vue'
import Err from '../views/err/404.vue'

const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'login',
      component: Login
    },
    {
      path:'/404',
      name:'404',
      component:Err
    },
    {
      path: '/home',
      name: 'home',
      component: () => import('../views/home/index.vue'),
      children:[
        {
          path:'list',
          name:'list',
          component:()=> import('@/views/home/components/list.vue')
        },
        {
          path:'person',
          name:'person',
          component:()=> import('@/views/home/components/personInfo.vue')
        },
        {
          path:'enroll',
          name:'enroll',
          component:() => import('@/views/home/components/enroll.vue')
        },
        {
          path:'check',
          name:'check',
          component:() => import('@/views/home/components/check.vue')
        },
        {
          path:'shop',
          name:'shop',
          component:() => import('@/views/home/components/shop.vue')
        }
      ]
    }
  ]
})
// 设置白名单，没有token也可以进入的页面
const whiteList = ['/','/404']
// 设置路由前置守卫
router.beforeEach((to, from, next) => {
  if (getToken()) {
      next(true)
  } else {
    // 如果没有token，执行以下代码，
    // if判断要进入的页面是否需要token，
    if (whiteList.includes(to.path)) {
      next(true)
    } else {
      next('/404')

    }
  }
})
export default router
